package com.hmy.contract.manager.exit;

import cn.hutool.json.JSONUtil;
import com.hmy.ccp.common.result.FeignResult;
import com.hmy.ccp.error.exception.HMYRuntimeException;
import com.hmy.fulfillment.api.bo.request.ExitFormBindContractUrlRequest;
import com.hmy.fulfillment.api.bo.request.ExitFormContractSignedSuccessRequest;
import com.hmy.fulfillment.api.bo.response.ExitFormBindContractUrlResponse;
import com.hmy.fulfillment.api.bo.response.ExitFormContractSignedSuccessResponse;
import com.hmy.fulfillment.api.client.ExitFormFeignClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author seven.qi 齐胜南
 * @version 1.0
 * @description
 * @date 2023/12/28 19:19
 */

@Component
@Slf4j
public class ExitFormFeignClientManager {

    @Resource
    private ExitFormFeignClient exitFormFeignClient;

    /**
     * 更新合同签署链接
     *
     * @param contractInfo 入参
     * @return {@link ExitFormBindContractUrlResponse}
     */
    public ExitFormBindContractUrlResponse bindExitFormContractSignUrl(ExitFormBindContractUrlRequest contractInfo) {
        log.info("ExitFormFeignClientManager.bindExitFormContractSignUrl params:{}", JSONUtil.toJsonPrettyStr(contractInfo));
        FeignResult<ExitFormBindContractUrlResponse> response = null;
        response = exitFormFeignClient.bindExitFormContractSignUrl(contractInfo);
        if (Objects.isNull(response)) {
            throw new HMYRuntimeException(502000029);
        }
        if (response.isFailed()) {
            throw new HMYRuntimeException(response.getMsgCode(), response.getErrorText());
        }
        log.info("ExitFormFeignClientManager.bindExitFormContractSignUrl response:{}", JSONUtil.toJsonPrettyStr(response));
        return response.getData();
    }

    /**
     * 合同签约完成回调
     *
     * @param contractInfo 更新合同状态入参
     * @return {@link ExitFormContractSignedSuccessResponse}
     */
    public ExitFormContractSignedSuccessResponse contractSignedSuccess(ExitFormContractSignedSuccessRequest contractInfo) {
        log.info("ExitFormFeignClientManager.contractSignedSuccess params:{}", JSONUtil.toJsonPrettyStr(contractInfo));
        FeignResult<ExitFormContractSignedSuccessResponse> response = null;
        response = exitFormFeignClient.contractSignedSuccess(contractInfo);
        if (Objects.isNull(response)) {
            throw new HMYRuntimeException(502000028);
        }
        if (response.isFailed()) {
            throw new HMYRuntimeException(response.getMsgCode(), response.getErrorText());
        }
        log.info("ExitFormFeignClientManager.contractSignedSuccess response:{}", JSONUtil.toJsonPrettyStr(response));
        return response.getData();
    }
}
